package stripesbook.view;

import java.util.ArrayList;
import java.util.List;
import org.stripesbook.rayures.view.ViewObject;
import org.stripesbook.rayures.view.stripes.SCheckBox;
import org.stripesbook.rayures.view.stripes.SForm;
import org.stripesbook.rayures.view.stripes.SSubmit;
import stripesbook.model.Role;
import stripesbook.model.User;

public class UserListForm extends SForm {
  private List<User> users;
  private List<Role> roles;

  public SForm users(List<User> users, List<Role> roles) {
    this.users = users;
    this.roles = roles;
    return getThis();
  }

  public List<User> getUsers() {
    return users;
  }

  public List<ViewObject<?>> getCheckboxesFor(User user, int index) {
    List<ViewObject<?>> list = new ArrayList<ViewObject<?>>();

    for (Role role : roles) {
      ViewObject<?> container = tag(ViewObject.class);
      SCheckBox checkbox = tag(SCheckBox.class).name("users[" + index + "].roles")
        .value(role).checked(user.getRoles());
      container.add(checkbox);
      container.add(role);
      list.add(container);
    }
    return list;
  }

  public SSubmit getSubmit() {
    return tag(SSubmit.class).name("save");
  }
}
